package com.easondongh.business.controller;

import com.easondongh.business.domain.GoodsImport;
import com.easondongh.business.service.GoodsImportService;
import com.easondongh.business.vo.GoodsImportQueryVo;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.domain.SysUser;
import com.easondongh.common.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/6/1 14:12
 */
@Slf4j
@RestController
@RequestMapping("/goodsImport")
public class GoodsImportController {
    @Autowired
    private GoodsImportService importService;

    /**
     * 新增商品进货
     * @param goodsImport
     * @return
     */
    @PostMapping("/addGoodsImport")
    public ResponseObj addGoodsImport(@RequestBody GoodsImport goodsImport) {
        if (goodsImport == null || goodsImport.getGoodsId() == null || goodsImport.getProviderId() == null
            || goodsImport.getNumber() == null) {
            log.warn("商品进货：新增时商品id、供应商id、商品数量均不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        goodsImport.setImportTime(new Date());
        SysUser loginUser = ShiroUtils.getCurrentLoginUser();
        goodsImport.setOperatePerson(loginUser.getName());
        this.importService.addGoodsImport(goodsImport);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 修改商品进货
     * @param goodsImport
     * @return
     */
    @PutMapping("updateGoodsImport")
    public ResponseObj updateGoodsImport(@RequestBody GoodsImport goodsImport) {
        if (goodsImport == null ||  goodsImport.getId() == null || goodsImport.getGoodsId() == null || goodsImport.getProviderId() == null
                || goodsImport.getNumber() == null) {
            log.warn("商品进货：更新时id、商品id、供应商id、商品数量均不能为空");
            return new ResponseObj(ResponseEnum.UPDATE_FAIL);
        }
        this.importService.updateGoodsImport(goodsImport);
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 批量删除商品进货
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.importService.deleteByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询商品进货
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<GoodsImport>> getByPage(@RequestBody BasePageQueryVo<GoodsImportQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<GoodsImport> result = this.importService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }
}
